import React, { Component } from 'react'
import axios from 'axios'
import {connect} from 'react-redux'
import {Vote} from '../../../types/vote'
import VoteItem from '../../../components/vote_item'

interface IProps{
    initList:(list:Vote[]) => void,
    voteList:Vote[]
}

class Home extends Component<IProps>{
    componentDidMount(){
        //请求数据
        axios.get('/api/voteList').then(res => {
            this.props.initList(res.data.data);
        })
    }
    render() {
        let {voteList} = this.props;
        return (
            <div>
                {voteList.length && voteList.map((item:Vote) => <VoteItem key={item.vid} item={item}></VoteItem>)}
            </div>
        )
    }
}

let stateMapToProps = (state:any) => {
    return {
        voteList:state.voteList.voteList
    }
}


let dispatchMapToProps = (dispatch:any) => {
    return {
        initList(list:Vote[]){
            dispatch({type:'INIT_LIST',list})
        }
    }
}

export default connect(stateMapToProps,dispatchMapToProps)(Home)